package com.xsherl.leetcode.solution;

public class ReverseInteger {

    /**
     * 因为题目不允许存储 64 位整数（有符号或无符号），所以我们需要提前判断结果是否会超出限制
     */
    public int reverse(int x) {
        int res = 0;
        while (x != 0){
            int residue = x % 10;
            if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && residue > 7)){
                return 0;
            }
            if (res < Integer.MIN_VALUE / 10 || (res == Integer.MIN_VALUE / 10 && residue > 8)){
                return 0;
            }
            res = res * 10 + residue;
            x /= 10;
        }
        return res;
    }

    public static void main(String[] args) {
        int x = 1534236469;
        System.out.println(Integer.MAX_VALUE + "--" + Integer.MIN_VALUE);
        System.out.println(new ReverseInteger().reverse(x));
    }
}
